//
//  YFStarVideoSubController.m
//  SquashSpark
//
//  Created by Coollang on 2017/11/3.
//  Copyright © 2017年 Coollang-YF. All rights reserved.
//

#import "YFStarVideoSubController.h"
#import "CoachVideo.h"
#import "YFStarVideoModel.h"
#import "YFStarVideoCell.h"
#import "UIView+Frame.h"
#import "UIScrollView+YFMJRefresh.h"
#import "JCVideoApi.h"
#import "WSProgressHUD.h"
#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>
#import "JCAVPlayerVC.h"

@interface YFStarVideoSubController ()

@property (nonatomic, strong) WSProgressHUD *hud;
@property (nonatomic, strong) NSArray <CoachVideo *> *videoArray;

@end

@implementation YFStarVideoSubController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.tableFooterView = [UIView new];
    self.view.backgroundColor = kBgThemeColor;
    self.title = self.videoModel.starName;
    [self loadData];
}

- (void)loadData{
    __weak typeof(self)weakSelf = self;
    [JCVideoApi getNodeVideoListForNodeID:_videoModel.nodeID complete:^(NSError *error, NSArray<CoachVideo *> *videoList) {
        if (error) {
            [weakSelf.hud showErrorWithString:error.domain dimissAfterSecond:2];
        }else{
            [weakSelf.hud dismiss];
            weakSelf.videoArray = videoList;
            [weakSelf.tableView reloadData];
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.videoArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YFStarVideoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YFStarVideoCell" forIndexPath:indexPath];
    cell.videoModel = _videoArray[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kAutoHei(103);
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSURL *url =  [NSURL URLWithString:_videoArray[indexPath.row].url];
    AVPlayer *player = [AVPlayer playerWithURL:url];
    JCAVPlayerVC *playerViewController = [JCAVPlayerVC new];
    playerViewController.player = player;
    [self presentViewController:playerViewController animated:YES completion:nil];
    [playerViewController.player play];
}

- (WSProgressHUD *)hud{
    if (_hud == nil) {
        _hud = [WSProgressHUD showOnView:self.view andString:@""];
        [_hud showWithMaskType:WSProgressHUDMaskTypeGradient];
        [_hud dismiss];
    }
    return _hud;
}


@end
